package com.hudson.store.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolConstant {

    public static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);
    public static ExecutorService executorService = Executors.newFixedThreadPool(5);

    public static void stop(){
        shutDown(scheduledThreadPoolExecutor);
        shutDown(executorService);
    }

    public static void shutDown(ExecutorService executorService){
        executorService.shutdown();
        try {
            // 等待最多30秒让现有任务完成
            if (!executorService.awaitTermination(30, TimeUnit.SECONDS)) {
                // 强制关闭
                executorService.shutdownNow();
                // 再等待5秒
                if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
                }
            }
        } catch (InterruptedException e) {
            executorService.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }
}
